/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jobportal.Dbconn;

/**
 *
 * @author project
 */
public class EditProfile extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        Connection con;
        Statement st;
        ResultSet rs;
        HttpSession session = request.getSession();
        String error = "";
        boolean flag = true;
        String dest = "editprofile.jsp";
        String compid = "";
        if (session.getAttribute("role") != null) {
            if (session.getAttribute("role").toString().equalsIgnoreCase("admin")) {
                dest = "../admin/manage_emp.jsp";
                compid = request.getParameter("compid");
            } else {
                compid = (String) session.getAttribute("username");
            }
        }
        PrintWriter out = response.getWriter();
        out.println(compid);
        String password, email, personname, designation, contact, fax, mobile, compname, address, state, city, type, cattype, sector, profile, url, sql;
        try {

            if (session.getAttribute("role") != null) {
            }
            con = Dbconn.getConn();
            st = con.createStatement();
            email = request.getParameter("email");

            personname = request.getParameter("personname");
            designation = request.getParameter("designation");
            contact = request.getParameter("contact");
            fax = request.getParameter("fax");
            mobile = request.getParameter("mobile");
            compname = request.getParameter("compname");
            address = request.getParameter("address");
            state = request.getParameter("state");
            city = request.getParameter("T8");
            type = request.getParameter("radiobutton");
            cattype = request.getParameter("indtype");
            sector = request.getParameter("compcat");
            profile = request.getParameter("profile");
            url = request.getParameter("url");
            sql = "update company set email=?,personname=?,designation=?,contact=?,fax=?,mobile=?,compname=?,address=?,state=?,city=?,type=?,cattype=?,sector=?,profile=?,url=? where compid='" + compid + "'";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, email);
            ps.setString(2, personname);
            ps.setString(3, designation);
            ps.setString(4, contact);
            ps.setString(5, fax);
            ps.setString(6, mobile);
            ps.setString(7, compname);
            ps.setString(8, address);
            ps.setString(9, state);
            ps.setString(10, city);
            ps.setString(11, type);
            ps.setString(12, cattype);
            ps.setString(13, sector);
            ps.setString(14, profile);
            ps.setString(15, url);
            if (ps.executeUpdate() > 0) {
                out.println("sucessfully updated");
                response.sendRedirect(dest + "?status=profile successfully updated");
            } else {
                out.println("updation failed");
                response.sendRedirect(dest + "?status=Updated failed");
            }

        } catch (Exception e) {
            out.println(e);
            response.sendRedirect(dest + "?status=Updated failed"+e);

        }

    }
}
